﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UseCaseAutoBinding.cs" company="">
//   
// </copyright>
// <summary>
//   The use case auto binding.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.Bindings
{
    using System.Reflection;

    using Libium.UseCases;
    using Libium.Views;

    /// <summary>
    /// The use case auto binding.
    /// </summary>
    /// <typeparam name="TUseCase">
    /// </typeparam>
    /// <typeparam name="TView">
    /// </typeparam>
    public class UseCaseAutoBinding<TUseCase, TView> : AutoBinding<TUseCase, TView>
        where TUseCase : IUseCase where TView : IView
    {
        // TODO Remove hack
        #region Fields

        /// <summary>
        ///   The _property 1.
        /// </summary>
        private PropertyInfo _property1;

        /// <summary>
        ///   The _property 2.
        /// </summary>
        private PropertyInfo _property2;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="UseCaseAutoBinding{TUseCase,TView}"/> class.
        /// </summary>
        /// <param name="service">
        /// The service. 
        /// </param>
        /// <param name="obj1">
        /// The obj 1. 
        /// </param>
        /// <param name="obj2">
        /// The obj 2. 
        /// </param>
        public UseCaseAutoBinding(IBindService service, TUseCase obj1, TView obj2)
            : base(service, obj1, obj2)
        {
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        ///   The bind.
        /// </summary>
        public override void Bind()
        {
            base.Bind();
            Object2.CopySource = Object1;
            Object2.PasteTarget = Object1;
            Object1.ModifiedChanged += uc =>
                {
                    if (_property1 != null && _property2 != null)
                    {
                        _property2.SetValue(Object2, _property1.GetValue(Object1, null), null);
                    }
					 };
				UseCaseBinding<IUseCase, IView>.EventsBinding(Object1, Object2);
        }

        #endregion

        #region Methods

        /// <summary>
        /// The can bind properties.
        /// </summary>
        /// <param name="property1">
        /// The property 1. 
        /// </param>
        /// <param name="property2">
        /// The property 2. 
        /// </param>
        /// <returns>
        /// The <see cref="bool"/> . 
        /// </returns>
        protected override bool CanBindProperties(PropertyInfo property1, PropertyInfo property2)
        {
            var name1 = property1.Name;
            var name2 = property2.Name;

            // TODO Remove hardcode
            if (name1 == "IsNew" && name1 == name2)
            {
                _property1 = property1;
                _property2 = property2;
                return true;
            }

            if (name1 == "Language" && name1 == name2)
            {
                return true;
            }

				if (name1 == "IsGallery" && name1 == name2)
            {
                return true;
            }

            if (name1 == "Information" && name1 == name2)
            {
                return true;
            }

            if (name1 == "AggregationVisible" && name1 == name2)
            {
                return true;
            }

            if (name1.EndsWith("UseCase") && name2.EndsWith("View"))
            {
                if (name1.Substring(0, name1.Length - 7) == name2.Substring(0, name2.Length - 4))
                {
                    return true;
                }
            }

            if (name2.EndsWith("View"))
            {
                if (name1 == name2.Substring(0, name2.Length - 4))
                {
                    return true;
                }
            }

            return false; // base.CanBindProperties(property1, property2);
        }

        #endregion
    }
}